#ifndef __UNITFORMATTER_H__
#define __UNITFORMATTER_H__

#include <iostream>

typedef int Amount;

template <class Unit>
struct UnitFormatter: Unit::UnitSeqVistor
    {
        UnitFormatter(std::ostream& out, Amount amountInBaseUnit) :
            out_m(out), amountInBaseUnit_m(amountInBaseUnit)
        {

        }

        void
        outputUnit(const Unit& unit)
        {
            out_m<<unit.convertFromBaseUnit(amountInBaseUnit_m)<<' '<<unit;
        }

        void
        access(const Unit* unit)
        {
            if(unit->convertFromBaseUnit(amountInBaseUnit_m)!=0)
            {
                outputUnit(*unit);
            }

            amountInBaseUnit_m = unit->convertOddmentFromBaseUnit(amountInBaseUnit_m);

            if(amountInBaseUnit_m!=0)
            {
                out_m<<' ';
            }
        }

    private:
        std::ostream& out_m;
        Amount amountInBaseUnit_m;
    };

template<class Unit>
void outputInBaseUnit(UnitFormatter<Unit>& formatter)
{
    formatter.outputUnit(Unit::getBaseUnit());
}

template<class Unit>
void outputInUnitSeq(UnitFormatter<Unit>& formatter)
{
    Unit::vistorUnitSeq(formatter);
}

#endif

